package com.mabang.medata.serviceImpl;

import java.math.BigDecimal;
import java.util.Date;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

import org.apache.cxf.common.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.mabang.medata.commons.ConstantStatus;
import com.mabang.medata.dao.CoverInsuranceMapper;
import com.mabang.medata.dao.CoverInsurancePolicyMapper;
import com.mabang.medata.model.BillNoResponse;
import com.mabang.medata.model.CoverInsurancePolicy;
import com.mabang.medata.procotol.BillProcotol;
import com.mabang.medata.service.BillQuoteService;
import com.mabang.medata.util.Utils;
import com.mabang.medata.util.XmlUtil;

@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
@Component
@WebService(name = "BillQuoteService", targetNamespace = "http://service.medata.mabang.com/")
public class BillQuoteServiceImpl implements BillQuoteService {
	Logger logger = Logger.getLogger(BillQuoteServiceImpl.class);
	@Autowired
	private CoverInsuranceMapper coverInsuranceMapper;
	@Autowired
	private CoverInsurancePolicyMapper coverInsurancePolicyMapper;
	@Override
	public String insertBillNo(String billQuote) {
		XmlUtil xmlUtil;
		Date date = new Date();
		String responseXml = null;
		String billNo="",quoteNo ="",message=ConstantStatus.MESSAGE_ERROR,status = ConstantStatus.SERVICE_UNAVAILABLE;
		try {
			xmlUtil = new XmlUtil(billQuote);
			billNo= xmlUtil.getSingleChildElementValue("proposal",
					"bill-no");
			quoteNo= xmlUtil.getSingleChildElementValue("proposal",
					"quote-no");
			if (StringUtils.isEmpty(quoteNo) || StringUtils.isEmpty(billNo)) {
				status =ConstantStatus.NO_CONTENT;
				message="报价单或投保单号不能为空";
			} else {
				logger.debug("解析后的保险单号：" + quoteNo);
				String tempBillNo = billNo;
				CoverInsurancePolicy cip = coverInsuranceMapper.getPolicyNmberAndIDByCode(quoteNo);
				if(cip != null){
				    int result=0;
                    try {
                        result = coverInsuranceMapper
                                .updatePolicyNmberByCoverInsuranceCode(date,
                                        quoteNo, tempBillNo);
                    } catch (Exception e) {
                        logger.error(e);
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                    tempBillNo = null;
                    if (result < 1) {
                        status = ConstantStatus.QUOTE_UNVALID;
                        if(cip.getChangeType() != null){
                            if(cip.getChangeType().intValue() == 1){
                                message = ConstantStatus.MESSAGE_AUTO;
                            }else if (cip.getChangeType().intValue() == 2) {
                                message = ConstantStatus.MESSAGE_DO;
                            }
                        }else{
                            message = ConstantStatus.MESSAGE_DO;
                        }
                    }else{
                        coverInsurancePolicyMapper.insert(cip);
                        status=ConstantStatus.OK;
                    }
				}else{
				    status = ConstantStatus.NOT_FOUND;
				    message = "报价单不存在";
				}
			}
		} catch (Exception e) {
			logger.error(e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} 
		BillNoResponse bnr = new BillNoResponse(billNo, quoteNo,
				Utils.dateToString(date), status,message);
		responseXml = BillProcotol.responseBillQuote(bnr);
		return responseXml;
	}
}
